package com.bjzsly.star3.xyh.manager;

import android.text.TextUtils;

import com.bjzsly.star3.xyh.auth.TxShareUtils;
import com.bjzsly.star3.xyh.base.AppClient;
import com.bjzsly.star3.xyh.utils.SimplePostHelper;

import org.json.JSONObject;

import rx.Observable;

/**
 * Created by ly on 2017/9/29.
 */

public class UserUtils {



    //微信相关
    public static void getWxAccessToken(String code, OnUserCallBack onUserCallBack) {
        String appKey = TxShareUtils.getInstance().getWxAppId();
        String appSecert = TxShareUtils.getInstance().getWxSecert();
        doExtraTask(AppClient.getApiService().getWxAccessToken(appKey, appSecert, code, "authorization_code"), onUserCallBack);
    }

    public static void getWxRefreshOpenId(String refreshToken, OnUserCallBack onUserCallBack) {
        String appKey = TxShareUtils.getInstance().getWxAppId();
        doExtraTask(AppClient.getApiService().getWxOpenId(appKey, refreshToken, "refresh_token"), onUserCallBack);
    }

    public static void getWxUserInfo(String accessToken, String openId, OnUserCallBack onUserCallBack) {
        doExtraTask(AppClient.getApiService().getWxUserInfo(accessToken, openId), onUserCallBack);
    }



//    public static void loginWx(String openId, String uuid,String reg_id, OnUserCallBack onUserCallBack) {
//        doTask(AppClient.getApiService().loginAuth(openId, "wx",uuid,reg_id), onUserCallBack);
//    }
//
//    public static void loginQQ(String openId,String uuid,String reg_id, OnUserCallBack onUserCallBack) {
//        doTask(AppClient.getApiService().loginAuth(openId, "qq",uuid,reg_id), onUserCallBack);
//    }

//    public static void doShareTask(String token, String taskId, String shareId, String t, OnUserCallBack onUserCallBack) {
//        doTask(AppClient.getApiService().doShareTask(token, taskId, shareId, t), onUserCallBack);
//    }

//    public static void readMsg(String token,String uuid) {
//        if(token!=null){
//            doTask(AppClient.getApiService().readMsg(token,uuid), null);
//        }else {
//            readMsg(uuid);
//        }
//    }


//    private static void doTask(Observable observable, final OnUserCallBack onUserCallBack) {
//        new SimplePostHelper().simplePost(observable, new SimplePostHelper.SimpleCallBack() {
//            @Override
//            public void onSuccess(Object data) {
//                if (onUserCallBack != null)
//                    onUserCallBack.onSuccess(data);
//            }
//
//            @Override
//            public void onError(String msg) {
//                if (onUserCallBack != null)
//                    onUserCallBack.onFail(msg);
//
//            }
//
//            @Override
//            public void onFail(String msg) {
//                if (onUserCallBack != null)
//                    onUserCallBack.onFail(msg);
//            }
//        });
//    }
//
//    private static void doTaskFin(Observable observable, final OnUserCallBackExtra onUserCallBack) {
//        new SimplePostHelper().simplePost(observable, new SimplePostHelper.SimpleCallBack() {
//            @Override
//            public void onSuccess(Object data) {
//                if (onUserCallBack != null)
//                    onUserCallBack.onSuccess(data);
//            }
//
//            @Override
//            public void onError(String msg) {
//                if (onUserCallBack != null)
//                    onUserCallBack.onError(msg);
//
//            }
//
//            @Override
//            public void onFail(String msg) {
//                if (onUserCallBack != null)
//                    onUserCallBack.onFail(msg);
//            }
//        });
//    }


    private static void doExtraTask(Observable observable, final OnUserCallBack onUserCallBack) {
        new SimplePostHelper().normalPost(observable, new SimplePostHelper.SimpleCallBack() {
            @Override
            public void onSuccess(Object data) {
                if (onUserCallBack != null)
                    onUserCallBack.onSuccess(data);
            }

            @Override
            public void onError(String msg) {
                if (onUserCallBack != null)
                    onUserCallBack.onFail(msg);

            }

            @Override
            public void onFail(String msg) {
                if (onUserCallBack != null)
                    onUserCallBack.onFail(msg);
            }
        });
    }


    private static String buildHeadToken(String token) {
        if (token != null) {
            return "Bearer " + token;
        }
        return null;
    }



    private static void addToJson(JSONObject json, String key, String value) {
        try {
            if (json != null && !TextUtils.isEmpty(key)) {
                if (!TextUtils.isEmpty(value))
                    json.put(key, value);
                else
                    json.put(key, value + "");
            }
        } catch (Exception e) {
        }

    }

    public static void doTestUse(String token, OnUserCallBack onUserCallBack) {
        doExtraTask(AppClient.getApiService().testJwt(token), onUserCallBack);
    }

}
